package com.lanou.dllo.mynovel.dataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.lanou.dllo.mynovel.bean.StoreUpdate;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/3.
 */
public class MySQLiteUpdateUtil {
    private SQLiteDatabase db;
    private Context context;
    private String type;

    public MySQLiteUpdateUtil(Context c) {//将activity对象传递过来
        context = c;
        initView();
    }

    private void initView() {//调用hepler类声明在哪个数据库进行操作
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(context, Constant.DATABASE_NAME, null, 1);
        db = helper.getWritableDatabase();
    }

    public void saveData(List<StoreUpdate.DataEntity.ReturnDataEntity> list) {//向数据库中保存数据
        for (StoreUpdate.DataEntity.ReturnDataEntity entity : list) {//在list集合中遍历 ReturnDataEntity 对象
            ContentValues values = new ContentValues();//用来操作数据库数据的类
            values.put(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_COVER, entity.getCover());
            values.put(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_NAME, entity.getName());
            values.put(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_NICKNAME, entity.getNickname());
            List<String> tags = entity.getTags();
            switch (tags.size()) {
                case 1:
                    values.put(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_CLASSIFYNAME, entity.getTags().get((0)));
                    break;
                case 2:
                    values.put(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_CLASSIFYNAME, entity.getTags().get((0)) + " " + entity.getTags().get((1)));
                    break;
                case 3:
                    values.put(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_CLASSIFYNAME, entity.getTags().get((0)) + " " + entity.getTags().get((1)) + " " + entity.getTags().get((2)));
                    break;
            }
            values.put(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_CLICK_TOTAL, entity.getClick_total());
            values.put(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_LIST_UPDATE_TIME, entity.getLast_update_time());
            values.put(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_LIST_UPDATE_CHAPTER_NAME, entity.getLast_update_chapter_name());
            db.insert(Constant.TABLE_NAME_UPDATE, null, values);
        }
    }

    public List<StoreUpdate.DataEntity.ReturnDataEntity> queryAll() {//查找所有
        Cursor cursor = db.query(Constant.TABLE_NAME_UPDATE, null, null, null, null, null, null);
        if (cursor != null) {
            List<StoreUpdate.DataEntity.ReturnDataEntity> entities = new ArrayList<>();
            while (cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_NAME));
                String cover = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_COVER));
                String nickName = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_NICKNAME));
                String total = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_CLICK_TOTAL));
                String time = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_LIST_UPDATE_TIME));
                String chapterName = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_LIST_UPDATE_CHAPTER_NAME));
                String tag = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_STOREUPDATE_RECYCLERVIEW_CLASSIFYNAME));
                List<String> tags = new ArrayList<>();
                tags.add(tag);

                StoreUpdate.DataEntity.ReturnDataEntity entity = new StoreUpdate.DataEntity.ReturnDataEntity();
                entity.setName(name);
                entity.setCover(cover);
                entity.setNickname(nickName);
                entity.setClick_total(total);
                entity.setLast_update_time(Integer.parseInt(time));
                entity.setLast_update_chapter_name(chapterName);
                entity.setTags(tags);
                entities.add(entity);
            }
            cursor.close();
            return entities;
        }
        return null;
    }
}
